package com.game.tictactoe;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 规则
 *
 * @Author xuehuanwu
 * @Date 2021/10/19 21:42
 * @Version 1.0
 */
public class Rule {

    public static final String VALID_INPUT = "^[1-9]$";
    public static final List<int[]> ruleList = new ArrayList<int[]>() {{
        add(new int[]{0, 0, 0, 1, 0, 2});
        add(new int[]{1, 0, 1, 1, 1, 2});
        add(new int[]{2, 0, 2, 1, 2, 2});
        add(new int[]{0, 0, 1, 0, 2, 0});
        add(new int[]{0, 1, 1, 1, 2, 1});
        add(new int[]{0, 2, 1, 2, 2, 2});
        add(new int[]{0, 0, 1, 1, 2, 2});
        add(new int[]{0, 2, 1, 1, 2, 0});
    }};

    /**
     * 检查输入值
     *
     * @param numInput
     * @return
     */
    public static String checkInput(String numInput) {
        while (true) {
            if (Pattern.matches(VALID_INPUT, numInput)) {
                return numInput;
            }
            System.out.print("Invalid input, Please re-enter your choice: ");
        }
    }

    /**
     * 选择
     *
     * @param attr
     * @param user
     * @return
     */
    public static String[][] choiceAttr(String[][] attr, User user) {
        boolean flag = false;
        for (int i = 0; i < attr.length; i++) {
            for (int j = 0; j < attr[i].length; j++) {
                if (attr[i][j].equals(user.getInputNum())) {
                    attr[i][j] = user.getRoleName();
                    flag = true;
                }
            }
        }
        // 判断是否属于无效选择
        if (!flag && !"".equals(user.getInputNum())) {
            System.out.println("Invalid choice!");
            return null;
        }
        return attr;
    }

    /**
     * 打印
     *
     * @param attr
     */
    public static void printAttr(String[][] attr) {
        for (int i = 0; i < attr.length; i++) {
            for (int j = 0; j < attr[i].length; j++) {
                if (Pattern.matches(VALID_INPUT, attr[i][j])) {
                    System.out.print(" ");
                } else {
                    System.out.print(attr[i][j]);
                }
                if (j < attr[i].length - 1) {
                    System.out.print(" | ");
                }
            }
            if (i < attr.length - 1) {
                System.out.println();
                System.out.println("---------");
            } else {
                System.out.println();
            }

        }
    }

    /**
     * 初始化规则
     *
     * @param attr
     * @return
     */
    public static boolean initRule(String[][] attr) {
        for (int[] rule : ruleList) {
            String one = attr[rule[0]][rule[1]];
            String two = attr[rule[2]][rule[3]];
            String three = attr[rule[4]][rule[5]];
            if (checkRule(one, two, three)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 检查规则
     *
     * @param one
     * @param two
     * @param three
     * @return
     */
    private static boolean checkRule(String one, String two, String three) {
        StringBuilder sb = new StringBuilder();
        sb.append(one).append(two).append(three);
        return "XXX".equals(sb.toString()) || "OOO".equals(sb.toString());
    }
}
